﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using log4net;

namespace MinerManager.Forms
{
	public partial class Reconnect : DevExpress.XtraEditors.XtraForm
	{
		ILog logger = LogManager.GetLogger(typeof(Program));

		private readonly int AwaitYesSecond = 30;//等待确定的秒数
		private bool IsFirstReconnect = true;//是否第一次重连

		public event EventHandler<EventArgs> OnHandle;
		public event EventHandler<EventArgs> OnCancel;

		public Reconnect()
		{
			InitializeComponent();
		}

		#region 窗体事件

		private void Reconnect_Load(object sender, EventArgs e)
		{
			CountDown();
		}

		private void Reconnect_FormClosing(object sender, FormClosingEventArgs e)
		{

		}

		#endregion

		#region void BtnYes_Click(object sender, EventArgs e)

		private void BtnYes_Click(object sender, EventArgs e)
		{
			Connection();
		}

		#endregion

		#region void BtnCancel_Click(object sender, EventArgs e) 

		private void BtnCancel_Click(object sender, EventArgs e)
		{
			OnCancel?.Invoke(this, new EventArgs());
			this.Close();
		}

		#endregion

		#region public void HandleFeedback(bool isSuccess) 操作结果反馈。

		private delegate void DelegateHandleFeedback(bool isSuccess);

		/// <summary>
		/// 操作结果反馈。
		/// </summary>
		/// <param name="isSuccess">是否操作成功</param>
		public void HandleFeedback(bool isSuccess)
		{
			if (this.InvokeRequired)
			{
				DelegateHandleFeedback d = new DelegateHandleFeedback(HandleFeedback);
				this.BeginInvoke(d, isSuccess);
			}
			else
			{
				ConnectionHandlerStop();
				if (isSuccess)
				{
					OnCancel?.Invoke(this, new EventArgs());
					CountDownSecond = -1;
					this.Close();
				}
				else
				{
					this.IsFirstReconnect = false;

					this.AllowCountDown = false;
					System.Threading.Thread.Sleep(1000);
					CountDown();
				}
			}
		}

		#endregion



		#region void CountDown() 开始倒计时。

		/// <summary>
		/// 开始倒计时。
		/// </summary>
		private void CountDown()
		{
			this.AllowCountDown = true;
			Task.Factory.StartNew(() =>
			{
				CountDownTimer();
			});
		}

		private bool AllowCountDown = false;//是否允许启用倒计时
		private int CountDownSecond = -1;//倒计时秒数
		private void CountDownTimer(bool isFirst = true)
		{
			this.CountDownSecond = this.AwaitYesSecond;

			while (this.CountDownSecond >= 0)
			{
				if (this.AllowCountDown && this.CountDownSecond == 0)
					Connection();

				if (!this.AllowCountDown || this.CountDownSecond == 0)
					System.Threading.Thread.CurrentThread.Abort();

				CountDownHandle(this.CountDownSecond);
				this.CountDownSecond--;
				System.Threading.Thread.Sleep(1000);
			}
		}

		private delegate void DelegateCountDownHandle(int countDownSecond);
		private void CountDownHandle(int countDownSecond)
		{
			if (this.InvokeRequired)
			{
				DelegateCountDownHandle d = new DelegateCountDownHandle(CountDownHandle);
				this.BeginInvoke(d, CountDownSecond);
			}
			else
			{
				LabelStatus.Text = !this.IsFirstReconnect ? "连接是失败。" : "";
				LabelStatus.Text += countDownSecond.ToString("00") + "秒后自动连接";
			}
		}

		#endregion

		#region void Connection() 执行连接操作。

		/// <summary>
		/// 执行连接操作。
		/// </summary>
		private void Connection()
		{
			Task.Factory.StartNew(() =>
			{
				ConnectionHandlerStart();
				OnHandle?.Invoke(this, new EventArgs());
			});
		}

		#endregion

		#region void ConnectionHandlerStart() 连接开始时的UI操作。

		/// <summary>
		/// 连接开始时的UI操作。
		/// </summary>
		private void ConnectionHandlerStart()
		{
			Task.Factory.StartNew(() =>
			{
				ConnectionHandlerStartHandle();
			});
		}

		private delegate void DelegateConnectionHandlerStart();
		private void ConnectionHandlerStartHandle()
		{
			if (this.InvokeRequired)
			{
				DelegateConnectionHandlerStart d = new DelegateConnectionHandlerStart(ConnectionHandlerStartHandle);
				this.BeginInvoke(d);
			}
			else
			{
				LabelStatus.Text = "正在连接服务器";
				BtnYes.Enabled = false;
			}
		}

		#endregion

		#region void ConnectionHandlerStop() 连接结束时的UI操作。

		/// <summary>
		/// 连接结束时的UI操作。
		/// </summary>
		private void ConnectionHandlerStop()
		{
			Task.Factory.StartNew(() =>
			{
				ConnectionHandlerStopHandle();
			});
		}

		private delegate void DelegateConnectionHandlerStop();
		private void ConnectionHandlerStopHandle()
		{
			if (this.InvokeRequired)
			{
				DelegateConnectionHandlerStop d = new DelegateConnectionHandlerStop(ConnectionHandlerStopHandle);
				this.BeginInvoke(d);
			}
			else
			{
				BtnYes.Enabled = true;
			}
		}

		#endregion
	}
}